﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Starfire3d.Data;

namespace Starfire3d
{
    public static class DataStore
    {
        private static Dictionary<string, GameObject> activeStore = new Dictionary<string, GameObject>();
        private static Dictionary<string, GameObject> deadStore = new Dictionary<string, GameObject>();

        public static void Clear()
        {
            activeStore.Clear();
            deadStore.Clear();
        }

        public static Dictionary<string, GameObject> Store
        {
            get { return activeStore; }
        }

        public static Dictionary<string, GameObject> DeadStore
        {
            get { return deadStore; }
        }

        public static void Remove(string key, bool moveToDead)
        {
            if (activeStore.ContainsKey(key.ToUpper()))
            {
                if (moveToDead)
                    deadStore.Add(key.ToUpper(), activeStore[key]);
 
                activeStore.Remove(key.ToUpper());
            }
        }

        public static void Add(GameObject obj)
        {
            if (!activeStore.ContainsKey(obj.Entity_Name.ToUpper()))
                activeStore.Add(obj.Entity_Name.ToUpper(), obj);
        }

        public static T[] GetAll<T>() where T : GameObject
        {
            return (T[])DataStore.Store.Select(o => o.Value).OfType<T>().ToArray();
        }

        public static T Get<T>(string name) where T : GameObject
        {
            if (activeStore.ContainsKey(name.ToUpper()))
                return activeStore[name.ToUpper()] as T;

            return null;
        }

        public static T GetDead<T>(string name) where T : GameObject
        {
            if (deadStore.ContainsKey(name.ToUpper()))
                return deadStore[name.ToUpper()] as T;

            return null;
        }
    }
}
